import time 
import pickle 
import friendfeed

FILTER_SRV = ['Twitter','YouTube','del.icio.us','Google Reader','Blog','Digg','Tumblr','FriendFeed']
STOPLIST = []


def getPublicFeed():
    """
    This is an generator/iterator function that allows us to listen to FF Public stream
    """
    service = friendfeed.FriendFeed()
    while(True):
        try:
            feed = service.fetch_public_feed()
            yield feed
        except:
            print "Some problem while fetching a feed"

def storeFeedsPickled(currentEntry,FH):
    """
    This is simple marshaling function
    """
    pickle.dump(currentEntry,FH)

def getTwitterUser(someText):
    """
    This basically generates twitter text feature
    """
    if(someText.find("@") == -1):
        return ''
    else:
        if(len(someText[someText.find('@')+1:].split()) > 0):
            return someText[someText.find('@')+1:].split()[0]
        else:
            return ''
def genWcStats(someLine):
    """
    This generates word statistics
    """
    someLine = someLine.replace("--"," ")
    someLine = someLine.replace("-"," ")
    someLine = someLine.replace("," , " ")
    someLine = someLine.replace("." ," ")
    someLine = someLine.replace("("," ")
    someLine = someLine.replace(")"," ")

    wc_results = {}
    
    for eachWord in someLine.split():    
        eachWord = eachWord.replace(";","")
        eachWord = eachWord.replace("/","")
        eachWord = eachWord.replace("|","")

        if(eachWord not in STOPLIST and eachWord != ""):
            eachWord = eachWord.lower()
            if(wc_results.has_key(eachWord) == False):
                wc_results[eachWord] = 0
            wc_results[eachWord] += 1
    return wc_results

def genWcStatsString(someDict):
    """
    This function is used to convert dictionary data to string
    we need this for shelve which only accepts strings 
    """
    result = ""
    for eachWord in someDict.keys():
        result += eachWord + ":"+ str(someDict[eachWord]) + "\t"
    return result 

def readStopList():
    """
    This reads stop list terms for a file
    """
    global STOPLIST
    fileToRead = open("stoplist.txt")
    for eachWord in fileToRead.readlines():
        eachWord = eachWord.strip()
        STOPLIST.append(eachWord)

def parseFeed(currentFeed):
    """
    This function is responsible for generating different features
    """
    for eachEntry in currentFeed['entries']:
        #Check if current service is to be monitored if its "*", we simply have to process all services

        if(eachEntry['service']['name'] in FILTER_SRV or FILTER_SRV[0] == "*"):
                
                serviceName = eachEntry['service']['name']

                if serviceName ==  'Twitter':
                        userInteraction = getTwitterUser(eachEntry['title'])
                        if(userInteraction != ''):
                            print('interaction'+"\t"+eachEntry['user']['nickname']+"\t"+userInteraction+"\t"+eachEntry['title'])

                if serviceName in ['Twitter','Jaiku','FriendFeed']:
                    print("wc_stats"+"\t"+eachEntry['user']['nickname']+"\t"+genWcStatsString(genWcStats(eachEntry['title'])))
                    
                if serviceName in ['YouTube','del.icio.us','Google Reader','Blog','Digg','Tumblr']:
                        resourceLink = eachEntry['link']
                        print(serviceName+"\t"+eachEntry['user']['nickname']+"\t"+resourceLink+"\t"+eachEntry['title'])

if __name__ == "__main__":

    readStopList()
    
    FFPubFeed = getPublicFeed()
    FFPubFeedStore = open('all_store','w')

    for eachCurrentFeed in FFPubFeed:
        parseFeed(eachCurrentFeed)
        storeFeedsPickled(eachCurrentFeed,FFPubFeedStore)
        time.sleep(15)
